package xyz.thoughtset.viewer.common.show.helper;

import org.springframework.core.annotation.AnnotationUtils;
import xyz.thoughtset.viewer.common.annotation.core.AMR;
import xyz.thoughtset.viewer.common.annotation.show.Input;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class SchemaHelper {

    public static Map editWebSelfStruct(Class clazz){
        Map<String, Object> map = new HashMap<>();
        for (Field field : clazz.getFields()) {
            // 获取方法上的所有注解
            Annotation[] annotations = field.getAnnotations();

            for (Annotation annotation : annotations) {
                Class annotationType = annotation.annotationType();
                if (AnnotationUtils.findAnnotation(annotationType, Input.class) == null) {
                    continue;
                }
                try {
                    Method[] methods = annotation.annotationType().getDeclaredMethods();
                    for (Method method : methods) {
                        Object value = method.invoke(annotation);
                        map.put(method.getName(), value);
                    }
                } catch (Exception e) {
                    throw new RuntimeException("Failed to convert annotation to map", e);
                }
                map.put("type",annToKebab(annotation));
            }
        }
        return map;
    }

    private static String annToKebab(Annotation ann){
        String simpleName = ann.annotationType().getSimpleName();
        return simpleName.replaceAll("([a-z0-9])([A-Z])", "-\\$1").toLowerCase();
    }

}
